package ink.ykb.demo.permission.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import ink.ykb.demo.config.CodeConstant;
import ink.ykb.demo.config.MessageUtil;
import ink.ykb.demo.config.ResponseMsg;
import ink.ykb.demo.permission.annotation.Function;
import ink.ykb.demo.permission.entity.Role;
import ink.ykb.demo.permission.service.RoleService;

/**
 * 角色相关服务
 * 
 * @author mading
 *
 */
@RestController
@RequestMapping("/role")
public class RoleController {

  @Autowired
  private RoleService roleService;
  @Autowired
  private MessageUtil messageUtil;

  /**
   * 添加角色
   * 
   * @param role
   */
  @Function("admin.role.add")
  @PostMapping(value = "/add")
  public ResponseMsg<String> add(Role role) {
    roleService.add(role);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }

  /**
   * 更新角色
   * 
   * @param role
   */
  @Function("admin.role.update")
  @PostMapping(value = "/update")
  public ResponseMsg<String> update(Role role) {
    roleService.update(role);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }

  /**
   * 删除角色
   * 
   * @param roleId
   */
  @Function("admin.role.delete")
  @PostMapping(value = "/delete")
  public ResponseMsg<String> delete(@RequestParam("roleId") Long roleId) {
    roleService.delete(roleId);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }

  /**
   * 查询角色列表
   * 
   * @return
   */
  @Function("admin.role.list")
  @GetMapping(value = "/list")
  public ResponseMsg<List<Role>> getRoleList() {
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS),
        roleService.getRoleList());
  }

  /**
   * 获取角色信息
   * 
   * @param roleId
   * @return
   */
  @Function("admin.role.get")
  @GetMapping(value = "/get")
  public ResponseMsg<Role> getRole(@RequestParam("roleId") Long roleId) {
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS),
        roleService.getRole(roleId));
  }
  
}
